﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading;
using System.Xml.Linq;
using System.Transactions;

namespace Test
{
    public class TestMgr
    {
        public static void DataProcessing()
        {
            //获取可用变更项目
            Type mtype = Type.GetType("Name", true);

            var instance = Activator.CreateInstance(mtype) as IDataProcessing;
            if (instance == null)
            {
                throw new Exception("变更未配置");
            }
            var result = instance.DataProcessingList();
            if (result != null && (result.TotalCount < 1 || result.DataProcessingList != null))
            {
                return;
            }
            int successCount = 0;
            int failCount = 0;
            foreach (var processingInfo in result.DataProcessingList)
            {
                using (TransactionScope tr = new TransactionScope(TransactionScopeOption.RequiresNew))
                {
                    try
                    {
                        //对获取数据校验操作
                        //eg.判断项目信息的合法性

                        //变更操作
                        //Update

                        //同步变更结果
                        instance.SycnDataProcessingResult(processingInfo);
                        successCount++;
                    }
                    catch (Exception ex)
                    {
                        //回写F：失败
                        instance.SycnDataProcessingResult(processingInfo, true, ex.Message);
                        failCount++;
                    }
                    finally
                    {
                        //记录变更日志
                    }
                    tr.Complete();
                }
            }

            //是否变更完成通知反馈
            if (instance.IsDataProcessingResultBack)
            {
                instance.DataProcessingResultBack(successCount, failCount);
            }

        }


    }
}
